package com.ysm.smfu.utils;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.ysm.smfu.bean.User;
import com.ysm.smfu.bean.UserInfos;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.welcome.SharedConfig;

/**
 * Created by Administrator on 2016/12/13.
 * 保存用户信息
 */
public class StoreInfo {
    private StoreInfo() {
    }

    private static StoreInfo storeInfo;

    public synchronized static StoreInfo getInstance() {
        if (storeInfo == null)
            storeInfo = new StoreInfo();
        return storeInfo;
    }

    /**
     * 保存用户所有信息
     *
     * @param currSP
     * @param userInfo
     */
    public void store(SharedPreferences currSP, User userInfo) {
        if (!TextUtils.isEmpty(userInfo.user.pwd))
            currSP.edit().putString(SpKey.NC, userInfo.user.pwd).commit();
        if (!TextUtils.isEmpty(userInfo.user.petName))
            currSP.edit().putString(SpKey.NC, userInfo.user.petName).commit();
        if (!TextUtils.isEmpty(userInfo.user.imageAddress) && !userInfo.user.imageAddress.startsWith("null?"))
            currSP.edit().putString(SpKey.IMAGEADDRESS, userInfo.user.imageAddress).commit();
        if (!TextUtils.isEmpty(userInfo.user.id))
            currSP.edit().putString(SpKey.USERID, userInfo.user.id).commit();
        if (!TextUtils.isEmpty(userInfo.user.loginId))
            currSP.edit().putString(SpKey.LOGINID, userInfo.user.loginId).commit();
        if (!TextUtils.isEmpty(userInfo.user.address))
            currSP.edit().putString(SpKey.DQ, userInfo.user.address).commit();
        if (!TextUtils.isEmpty(userInfo.user.hxId))
            currSP.edit().putString(SpKey.HXID, userInfo.user.hxId).commit();
        if (!TextUtils.isEmpty(userInfo.user.lastloginTime))
            currSP.edit().putString(SpKey.LAST_TIME, userInfo.user.lastloginTime).commit();
        if (!TextUtils.isEmpty(userInfo.user.address))
            currSP.edit().putString(SpKey.ADDRESS, userInfo.user.address).commit();
        if (!TextUtils.isEmpty(userInfo.user.birthday))
            currSP.edit().putString(SpKey.SR, userInfo.user.birthday).commit();
        if (!TextUtils.isEmpty(userInfo.user.sex) && TextUtils.equals(userInfo.user.sex, "1"))
            currSP.edit().putString(SpKey.SEX, "男").commit();
        else if (!TextUtils.isEmpty(userInfo.user.sex) && TextUtils.equals(userInfo.user.sex, "0"))
            currSP.edit().putString(SpKey.SEX, "女").commit();
        if (!TextUtils.isEmpty(userInfo.user.phone))
            currSP.edit().putString(SpKey.PHONE, userInfo.user.phone).commit();
        if (!TextUtils.isEmpty(userInfo.user.is_business))
            currSP.edit().putString(SpKey.IS_BUSINESS, userInfo.user.is_business).commit();
        if (!TextUtils.isEmpty(userInfo.user.level))
//        用户等级
            currSP.edit().putString(SpKey.LEVEL, userInfo.user.level).commit();
        //        邀请好友需要
        if (!TextUtils.isEmpty(userInfo.user.codePre))
            currSP.edit().putString(SpKey.CODEPRE, userInfo.user.codePre).commit();
        if (!TextUtils.isEmpty(userInfo.user.codeSuf))
            currSP.edit().putString(SpKey.CODESUF, userInfo.user.codeSuf).commit();
        if (userInfo.merchant != null) {
            if (!TextUtils.isEmpty(userInfo.merchant.name))
                currSP.edit().putString(SpKey.SHANGHUNAME, userInfo.merchant.name).commit();
            if (!TextUtils.isEmpty(userInfo.merchant.id))
                currSP.edit().putString(SpKey.BUSSINESSID, userInfo.merchant.id).commit();
            if (!TextUtils.isEmpty(userInfo.merchant.headPhoto))
                currSP.edit().putString(SpKey.HEADPHOTO, userInfo.merchant.headPhoto).commit();
            currSP.edit().putString(SpKey.PROXY, userInfo.user.agent);
        }

        //      将获取到的图片url转换成bitmap并保存到本地
//      将获取到的图片url转换成bitmap并保存到本地
//        if (!TextUtils.isEmpty(userInfo.user.imageAddress) && !userInfo.user.imageAddress.startsWith("null?")) {
////            头像地址不为空，保存到本地
//            OkHttpUtils
//                    .get()
//                    .url(userInfo.user.imageAddress)
//                    .build()
//                    .execute(new BitmapCallback() {
//                        @Override
//                        public void onError(Call call, Exception e, int id) {
//                            cancleDialog();
//                            UIUtils.startActivity(MainActivity.class);
//                            finish();
//                        }
//
//                        @Override
//                        public void onResponse(Bitmap bitmap, int id) {
//                            MyImageBitmapUtil.saveBitmapLoc(bitmap, numPhone);
////                            商户头像不为空，保存到本地
//                            if (userInfo.merchant != null) {
//                                if (!TextUtils.isEmpty(userInfo.merchant.headPhoto)) {
//                                    OkHttpUtils
//                                            .get()
//                                            .url(userInfo.merchant.headPhoto)
//                                            .build()
//                                            .execute(new BitmapCallback() {
//                                                @Override
//                                                public void onError(Call call, Exception e, int id) {
//                                                    cancleDialog();
//                                                    UIUtils.startActivity(MainActivity.class);
//                                                    finish();
//                                                }
//
//                                                @Override
//                                                public void onResponse(Bitmap headPhoto, int id) {
//                                                    //  保存商户头像,防止商户名和用户名一致，不使用商户名保存
//                                                    MyImageBitmapUtil.saveBitmapLoc(headPhoto, numPhone + SpKey.BUSINESS);
//                                                    // 取消进度条
//                                                    cancleDialog();
//                                                    UIUtils.startActivity(MainActivity.class);
//                                                    finish();
//                                                }
//                                            });
//                                } else {
//                                    // 取消进度条
//                                    cancleDialog();
//                                    UIUtils.startActivity(MainActivity.class);
//                                    finish();
//                                }
//                            } else {
//                                // 取消进度条
//                                cancleDialog();
//                                UIUtils.startActivity(MainActivity.class);
//                                finish();
//                            }
//                        }
//                    });
    }

    /**
     * 清空位置信息
     *
     * @param sp
     */
    public void clear(SharedPreferences sp) {
        sp.edit().putString(SpKey.SELECID, "").commit();
        sp.edit().putString(SpKey.LOCID, "").commit();
        sp.edit().putString(SpKey.BDLOC, "").commit();
        sp.edit().putString(SpKey.SELECCITY, "").commit();
        sp.edit().putString(SpKey.LAT, "").commit();
        sp.edit().putString(SpKey.LON, "").commit();
        String name = sp.getString(SpKey.NAME, "");
//        将支付相关清空
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        currShared.edit().putString(SpKey.PAY_PRICE, "").commit();
        currShared.edit().putString(SpKey.Pay_BUSSINESSID, "").commit();
        currShared.edit().putString(SpKey.JIFEN, "").commit();
        currShared.edit().putString(SpKey.REMARK, "").commit();
        currShared.edit().putString("payInfo_price", "").commit();
        currShared.edit().putString("payInfo_returnPoints", "").commit();
    }

    /**
     * 存储基本信息
     */
    public void storeBasicInfo(SharedPreferences currSP, User userInfo) {
//                将用户信息保存到本地
        currSP.edit().putString(SpKey.USERID, userInfo.user.id).commit();
        currSP.edit().putString(SpKey.LOGINID, userInfo.user.loginId).commit();
        currSP.edit().putString(SpKey.HXID, userInfo.user.hxId).commit();
        currSP.edit().putString(SpKey.LAST_TIME, userInfo.user.lastloginTime).commit();
        currSP.edit().putString(SpKey.PHONE, userInfo.user.phone).commit();
        currSP.edit().putString(SpKey.PWD, userInfo.user.pwd).commit();
        //        邀请好友需要
        if (!TextUtils.isEmpty(userInfo.user.codePre))
            currSP.edit().putString(SpKey.CODEPRE, userInfo.user.codePre).commit();
        if (!TextUtils.isEmpty(userInfo.user.codeSuf))
            currSP.edit().putString(SpKey.CODESUF, userInfo.user.codeSuf).commit();
    }

    /**
     * 清理支付信息
     *
     * @param currShared
     */
    public void clearPayInfo(SharedPreferences currShared) {
        currShared.edit().putString(SpKey.Pay_BUSSINESSID, "").commit();
        currShared.edit().putString(SpKey.REMARK, "").commit();
        currShared.edit().putString(SpKey.JIFEN, "").commit();
        currShared.edit().putString(SpKey.PAY_PRICE, "").commit();
    }

    /**
     * 退出登录时，清空用户信息
     */
    public void clearInfo(SharedPreferences sp, SharedPreferences currShared) {
        PrintUtil.printRespones("清空信息");
        BaseProtocal.setNull();
        sp.edit().putString(SpKey.NAME, "").commit();
        currShared.edit().putString(SpKey.PWD, "").commit();
        currShared.edit().putString(SpKey.PHONE, "").commit();
        currShared.edit().putString(SpKey.DQ, "").commit();
        currShared.edit().putString(SpKey.NC, "").commit();
        currShared.edit().putString(SpKey.SR, "").commit();
        currShared.edit().putString(SpKey.SEX, "").commit();
        currShared.edit().putString(SpKey.CODEPRE, "").commit();
        currShared.edit().putString(SpKey.CODESUF, "").commit();
        currShared.edit().putString(SpKey.BUSSINESSID, "").commit();
        currShared.edit().putString(SpKey.HEADPHOTO, "").commit();
        currShared.edit().putString(SpKey.SHANGHUNAME, "").commit();
        currShared.edit().putString(SpKey.LAST_TIME, "").commit();
        currShared.edit().putString(SpKey.ADDRESS, "").commit();
        currShared.edit().putString(SpKey.USERID, "").commit();
        currShared.edit().putString(SpKey.LOGINID, "").commit();
        currShared.edit().putString(SpKey.HXID, "").commit();
        currShared.edit().putString(SpKey.IS_BUSINESS, "").commit();
        currShared.edit().putString(SpKey.NC, "").commit();
        currShared.edit().putString(SpKey.IMAGEADDRESS, "").commit();
        currShared.edit().putString(SpKey.LEVEL, "0").commit();
        currShared.edit().putString(SpKey.BUSSINESSID, "").commit();
        currShared.edit().putString(SpKey.PROXY, "").commit();
    }

    /**
     * 商城登陆时，保存用户信息
     */
    public void storeUserInfo(SharedPreferences currSP, UserInfos userInfos) {
        UserInfos.UserInfo userInfo = userInfos.userInfo;
        if (!TextUtils.isEmpty(userInfo.pwd))
            currSP.edit().putString(SpKey.PWD, userInfo.pwd).commit();
        if (!TextUtils.isEmpty(userInfo.petName))
            currSP.edit().putString(SpKey.NC, userInfo.petName).commit();
        if (!TextUtils.isEmpty(userInfo.imageAddress) && !userInfo.imageAddress.startsWith("null?"))
            currSP.edit().putString(SpKey.IMAGEADDRESS, userInfo.imageAddress).commit();
        if (!TextUtils.isEmpty(userInfo.id))
            currSP.edit().putString(SpKey.USERID, userInfo.id).commit();
        if (!TextUtils.isEmpty(userInfo.loginId))
            currSP.edit().putString(SpKey.LOGINID, userInfo.loginId).commit();
        if (!TextUtils.isEmpty(userInfo.address))
            currSP.edit().putString(SpKey.DQ, userInfo.address).commit();
        if (!TextUtils.isEmpty(userInfo.hxId))
            currSP.edit().putString(SpKey.HXID, userInfo.hxId).commit();
        if (!TextUtils.isEmpty(userInfo.lastloginTime))
            currSP.edit().putString(SpKey.LAST_TIME, userInfo.lastloginTime).commit();
        if (!TextUtils.isEmpty(userInfo.address))
            currSP.edit().putString(SpKey.ADDRESS, userInfo.address).commit();
        if (!TextUtils.isEmpty(userInfo.birthday))
            currSP.edit().putString(SpKey.SR, userInfo.birthday).commit();
        if (!TextUtils.isEmpty(userInfo.sex) && TextUtils.equals(userInfo.sex, "1"))
            currSP.edit().putString(SpKey.SEX, "男").commit();
        else if (!TextUtils.isEmpty(userInfo.sex) && TextUtils.equals(userInfo.sex, "0"))
            currSP.edit().putString(SpKey.SEX, "女").commit();
        if (!TextUtils.isEmpty(userInfo.userName))
            currSP.edit().putString(SpKey.PHONE, userInfo.userName).commit();
        if (!TextUtils.isEmpty(userInfo.is_business))
            currSP.edit().putString(SpKey.IS_BUSINESS, userInfo.is_business).commit();
        if (!TextUtils.isEmpty(userInfo.level))
//        用户等级
            currSP.edit().putString(SpKey.LEVEL, userInfo.level).commit();
        //        邀请好友需要
        if (!TextUtils.isEmpty(userInfo.codePre))
            currSP.edit().putString(SpKey.CODEPRE, userInfo.codePre).commit();
        if (!TextUtils.isEmpty(userInfo.codeSuf))
            currSP.edit().putString(SpKey.CODESUF, userInfo.codeSuf).commit();
        UserInfos.Merchant merchant = userInfos.merchant;
        if (merchant != null) {
            if (!TextUtils.isEmpty(merchant.name))
                currSP.edit().putString(SpKey.SHANGHUNAME, merchant.name).commit();
            if (!TextUtils.isEmpty(merchant.id))
                currSP.edit().putString(SpKey.BUSSINESSID, merchant.id).commit();
            if (!TextUtils.isEmpty(merchant.headPhoto))
                currSP.edit().putString(SpKey.HEADPHOTO, merchant.headPhoto).commit();
        }
    }
}
